package com.wang.controller;

import com.wang.luntan.common.pojo.ApiResponse;
import com.wang.luntan.common.util.PageInfoUtil;
import com.wang.luntan.common.web.controller.BaseController;
import com.wang.luntan.users.pojo.AtAdmins;
import com.wang.luntan.users.pojo.AtOpinionType;
import com.wang.luntan.users.pojo.AtUsers;
import com.wang.luntan.users.service.IOpinionDbService;
import com.wang.luntan.users.service.IUsersDbService;
import com.wang.luntan.users.service.IUsersOperService;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/***
 * 管理员页面
 *
 */
@Controller
@RequestMapping(value = "/back/users/")
@Log4j2
public class AdminsBackController extends BaseController {

    @Autowired
    private IUsersDbService usersDbService ;
    @Autowired
    private IUsersOperService usersOperService ;

    /**
     * 用户列表页面
     *
     * @return
     */
    @RequestMapping("/usersList")
    public String usersList(HttpServletRequest request, Model model) {
        log.info("--usersList--");
        /* 分页信息 */
        PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
        /* 搜索条件,生成公共的搜索条件 */
        Map<String, Object> condMap = this.proccedSearch(request);
        /* 查询数据库
         * 所有的数据以JSON的形式返回
         *  */
        ApiResponse<AtUsers> response = this.usersOperService.findCondListusersService(pageInfoUtil, condMap);
        /* 将结果存储到Request中 */
        model.addAttribute("response", response.toJSON());
        return "/back/users/usersList";
    }
    /**
     * 更新
     *
     * @return
     */
    @RequestMapping("/usersUpdate")
    public String usersUpdate(String id, String operType, Model model) {
        log.info("--adminsUpdate--");
        /* 准备查询条件 */
        Map<String, Object> condMap = new HashMap<String, Object>();
        condMap.put("id", id);
        /* 查询数据库 */
        ApiResponse<AtUsers> response = this.usersOperService.findOneAtUsersService(condMap);
        /* 存储request */
        model.addAttribute("response", response.toJSON());

        if ("update".equalsIgnoreCase(operType)) {
            return "/back/users/adminsUpdate";
        }
        return "/back/users/usersUpdate";
    }
    /**
     * 管理员列表页面
     *
     * @return
     */
    @RequestMapping("/adminsList")
    public String adminsList(HttpServletRequest request, Model model) {
        log.info("--adminsList--");
        /* 分页信息 */
        PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
        /* 搜索条件,生成公共的搜索条件 */
        Map<String, Object> condMap = this.proccedSearch(request);
        /* 查询数据库
         * 所有的数据以JSON的形式返回
         *  */
        ApiResponse<AtAdmins> response = this.usersDbService.findCondListAdminsService(pageInfoUtil, condMap);
        /* 将结果存储到Request中 */
        model.addAttribute("response", response.toJSON());
        return "/back/users/adminsList";
    }



    /**
     * 打开添加朝代页面
     *
     * @return
     */
    @RequestMapping("/adminsInsert")
    public String adminsInsert() {
        log.info("--adminsInsert--");
        return "/back/users/adminsInsert";
    }

    /**
     * 添加朝代提交操作
     *
     * @return
     */
    @RequestMapping(value = "/adminsInsertSubmit", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String adminsInsertSubmit(String pubTimeStr, AtAdmins obj) {
        log.info("--adminsInsertSubmit--");
        /* 为对象赋值 */
        obj.setCreateTime(new Date());
        obj.setUpdateTime(new Date());
        /* 保存结果 */
        ApiResponse response = this.usersDbService.saveOneAdminsService(obj);
        log.info("--朝代添加返回结果:{}--", response.toJSON());
        return response.toJSON().toJSONString();
    }

    /**
     * 更新
     *
     * @return
     */
    @RequestMapping("/adminsUpdate")
    public String adminsUpdate(String id, String operType, Model model) {
        log.info("--adminsUpdate--");
        /* 准备查询条件 */
        Map<String, Object> condMap = new HashMap<String, Object>();
        condMap.put("id", id);
        /* 查询数据库 */
        ApiResponse<AtAdmins> response = this.usersDbService.findOneAdminsService(condMap);
        /* 存储request */
        model.addAttribute("response", response.toJSON());

        if ("update".equalsIgnoreCase(operType)) {
            return "/back/users/adminsUpdate";
        }
        return "/back/users/adminsUpdate";
    }

    /**
     * 更新
     *
     * @return
     */
    @RequestMapping(value = "/adminsUpdateSubmit", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String adminsUpdateSubmit(String id,
                                          HttpServletRequest request, Model model) {
        log.info("--adminsUpdateSubmit--");
        /* 准备查询条件 */
        Map<String, Object> condMap = new HashMap<String, Object>();
        condMap.put("id", id);
        /* 查询数据库 */
        ApiResponse<AtAdmins> response = this.usersDbService.findOneAdminsService(condMap);
        /* 获取java对象 */
        AtAdmins demoDynasty = response.getDataOneJava();

        /* 接收参数 */
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        String qq = request.getParameter("qq");
        String status = request.getParameter("status");
        String pubTimeStr = request.getParameter("pubTimeStr");
        /* 设置属性 */
        demoDynasty.setEmail(email);
        demoDynasty.setPhone(phone);
        demoDynasty.setQq(qq);
        demoDynasty.setStatus(Byte.valueOf(status));
        demoDynasty.setPubTime(this.dateFormatUtil.strDateTime(pubTimeStr));
        demoDynasty.setUpdateTime(new Date());
        /* 数据库操作 */
        ApiResponse dbRes = usersDbService.updateOneAdminsService(demoDynasty);
        log.info("--朝代添加返回结果:{}--", dbRes.toJSON());
        return dbRes.toJSON().toJSONString();
    }
    /**
     * 重置密碼
     *
     * @return
     */
    @RequestMapping("/adminsUpdatePassword")
    public String adminsUpdatePassword(String id, String operType, Model model) {
        log.info("--adminsUpdatePassword--");
        /* 准备查询条件 */
        Map<String, Object> condMap = new HashMap<String, Object>();
        condMap.put("id", id);
        /* 查询数据库 */
        ApiResponse<AtAdmins> response = this.usersDbService.findOneAdminsService(condMap);
        /* 存储request */
        model.addAttribute("response", response.toJSON());

        if ("update".equalsIgnoreCase(operType)) {
            return "/back/users/adminsUpdatePassword";
        }
        return "/back/users/adminsUpdatePassword";
    }
    /**
     * 重置密码提交
     *
     * @return
     */
    @RequestMapping(value = "/adminsUpdateSubmitPassword", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String adminsUpdateSubmitPassword(String id,
                                     HttpServletRequest request, Model model) {
        log.info("--adminsUpdateSubmitPassword--");
        /* 准备查询条件 */
        Map<String, Object> condMap = new HashMap<String, Object>();
        condMap.put("id", id);
        /* 查询数据库 */
        ApiResponse<AtAdmins> response = this.usersDbService.findOneAdminsService(condMap);
        /* 获取java对象 */
        AtAdmins demoDynasty = response.getDataOneJava();
        /* 接收参数 */
        String password = request.getParameter("password");
        /* 设置属性 */
        demoDynasty.setPassword(password);
        demoDynasty.setUpdateTime(new Date());
        /* 数据库操作 */
        ApiResponse dbRes = usersDbService.updateOneAdminsService(demoDynasty);
        log.info("--朝代添加返回结果:{}--", dbRes.toJSON());
        return dbRes.toJSON().toJSONString();
    }
}

